﻿using AutoMapper;
using B.S.Warehouse.Domain.goods;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.GoodsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.GoodsHandler
{
    public class DelGoodsHandler : IRequestHandler<DelGoodsCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;
        public DelGoodsHandler(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(DelGoodsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var list = await db.Queryable<GoodsModel>().FirstAsync(x => x.GoodsModelId == request.GoodsModelId);
            if (list != null)
            {
                list.IsDel = true;
                result.data = await db.Updateable<GoodsModel>(list).ExecuteCommandAsync();
                if (result.data > 0)
                {
                    result.Code = ApiEnum.删除成功;
                    result.MAG = ApiEnum.删除成功.ToString();
                }
                else
                {
                    result.Code = ApiEnum.删除成功;
                    result.MAG = ApiEnum.删除成功.ToString();
                }
            }
            return result;
        }
    }
}
